package com.tuozixuan.javafx;

import java.time.LocalDate;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableColumn.CellDataFeatures;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.util.Callback;

public class HelloWorldStage extends Application {

	public static void main(String[] args) {
		Application.launch(args);

	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		BorderPane root = new BorderPane();
		Scene scene = new Scene(root, 1000, 500, Color.WHITE);

		/* ===================菜单======================== */
		MenuBar menuBar = new MenuBar();
		menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
		root.setTop(menuBar);

		Menu fileMenu = new Menu("File");
		MenuItem newMenuItem = new MenuItem("New");
		MenuItem saveMenuItem = new MenuItem("Save");
		MenuItem exitMenuItem = new MenuItem("Exit");
		exitMenuItem.setOnAction(actionEvent -> {
			Platform.exit();
		});

		fileMenu.getItems().addAll(newMenuItem, saveMenuItem, new SeparatorMenuItem(), exitMenuItem);
		menuBar.getMenus().addAll(fileMenu);
		
		/* ===================树形列表======================== */
		TreeItem<String> treeItem1 = new TreeItem<>("Note1");
		TreeItem<String> treeItem2 = new TreeItem<>("Note2");
		TreeItem<String> treeItem3 = new TreeItem<>("Note3");
		
		TreeItem<String> treeItemRoot = new TreeItem<>("NoteRoot");
		treeItemRoot.setExpanded(true);
		treeItemRoot.getChildren().setAll(treeItem1, treeItem2, treeItem3);
		
		TreeItem<String> treeItem4 = new TreeItem<>("Note4");
		TreeItem<String> treeItem5 = new TreeItem<>("Note5");
		TreeItem<String> treeItem6 = new TreeItem<>("Note6xxxxxxxxxxxxxxxxxxxxxxxxxxx");
		
		treeItem1.getChildren().addAll(treeItem4, treeItem5, treeItem6);
		
//		TreeItem<String> treeItemRoot1 = new TreeItem<>("NoteRoot1");
//		treeItemRoot1.setExpanded(true);
//		treeItemRoot1.getChildren().setAll(treeItem4, treeItem5, treeItem6);
		
		TreeTableColumn<String, String> treeTableColumn = new TreeTableColumn<>("Column");
		treeTableColumn.setPrefWidth(150);
		treeTableColumn.setCellValueFactory((CellDataFeatures<String, String> p) -> new ReadOnlyStringWrapper(p.getValue().getValue()));
		
		
		TreeTableView<String> treeTableView = new TreeTableView<String>(treeItemRoot);
		treeTableView.getColumns().addAll(treeTableColumn);
		treeTableView.setShowRoot(false);
		treeTableView.setPrefWidth(152);
		
		root.setLeft(treeTableView);
		
		/* ===================表格======================== */
		TableView table = new TableView();
		
		TableColumn firstNameCol = new TableColumn("First Name");
		firstNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("lastName"));
        TableColumn emailCol = new TableColumn("Email");
        emailCol.setCellValueFactory(new PropertyValueFactory<Person,String>("email"));
        
        ObservableList<Person> tableData = this.buildTableData();
        table.setItems(tableData);
        
        Callback<TableColumn, TableCell> cellFactory = 
        	    new Callback<TableColumn, TableCell>() {
        	        public TableCell call(TableColumn p) {
        	            return new EditingCell();
        	    }
        	};

        	firstNameCol.setCellFactory(cellFactory);
        	lastNameCol.setCellFactory(cellFactory);
        	emailCol.setCellFactory(cellFactory);
        	
        	table.setEditable(true);

        	//Modifying the firstName property
        	firstNameCol.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
        	    @Override public void handle(CellEditEvent<Person, String> t) {
        	        ((Person)t.getTableView().getItems().get(
        	            t.getTablePosition().getRow())).setFirstName(t.getNewValue());
        	    }
        	});

        	//Modifying the lastName property
        	lastNameCol.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
        	    @Override public void handle(CellEditEvent<Person, String> t) {
        	        ((Person)t.getTableView().getItems().get(
        	            t.getTablePosition().getRow())).setLastName(t.getNewValue());
        	    }
        	}); 

        	//Modifying the email property
        	emailCol.setOnEditCommit(new EventHandler<CellEditEvent<Person, String>>() {
        	    @Override public void handle(CellEditEvent<Person, String> t) {
        	        ((Person)t.getTableView().getItems().get(
        	            t.getTablePosition().getRow())).setEmail(t.getNewValue());
        	     }
        	});
        
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
        //table.setPrefWidth(800);
        table.autosize();
        //root.setCenter(table);
        
        /* ===================文本区域======================== */
        TextArea textArea = new TextArea();
        textArea.autosize();
        
        DatePicker checkInDatePicker = new DatePicker();
        checkInDatePicker.setValue(LocalDate.of(2022, 8, 16));
        //DatePicker checkInDatePicker = new DatePicker(LocalDate.of(2022, 8, 15));
        //checkInDatePicker.setShowWeekNumbers(false);

        
        //root.setBottom(textArea);
        
        ChoiceBox<String> choiceBox = new ChoiceBox<String>(
                FXCollections.observableArrayList("firstName", "lastName", "email"));
        ChoiceBox<String> operateChoiceBox = new ChoiceBox<String>(
                FXCollections.observableArrayList(">", ">=", "<", "<=", "=", "包含", "为空", "不为空", "like"));
        
        TextField textField = new TextField();
        textField.setText("text");
        
        ToggleButton toggleButton = new ToggleButton("Press me");
        
        ToggleGroup toggleGroup = new ToggleGroup();
        ToggleButton tb1 = new ToggleButton("High");
        tb1.setUserData("High");
        tb1.setToggleGroup(toggleGroup);
        tb1.setSelected(true);
        ToggleButton tb2 = new ToggleButton("Medium");
        tb2.setUserData("Medium");
        tb2.setToggleGroup(toggleGroup);
        ToggleButton tb3 = new ToggleButton("Low");
        tb3.setUserData("Low");
        tb3.setToggleGroup(toggleGroup);
        
        toggleGroup.selectedToggleProperty().addListener((ObservableValue<? extends Toggle> ov, Toggle toggle1, Toggle new_toggle) -> {
            if (new_toggle == null)
                System.out.println("default value");
            else
                System.out.println(toggleGroup.getSelectedToggle().getUserData());
        });
        
        Button queryButton =  new Button();
        queryButton.setText("查询");
        queryButton.setOnAction((ActionEvent event) -> {
        	System.out.println("query....");
        });
        
        Hyperlink hyperLink = new Hyperlink("http://www.onelink.love");
        hyperLink.setOnAction(event -> {
        	System.out.println("link clicked");
        });

        
        HBox hbox = new HBox();
        hbox.getChildren().add(checkInDatePicker);
        hbox.getChildren().add(choiceBox);
        hbox.getChildren().add(operateChoiceBox);
        hbox.getChildren().add(textField);
        hbox.getChildren().add(toggleButton);
        hbox.getChildren().add(tb1);
        hbox.getChildren().add(tb2);
        hbox.getChildren().add(tb3);
        hbox.getChildren().add(queryButton);
        hbox.getChildren().add(hyperLink);
        
        TitledPane titledPane = new TitledPane("My Title", new CheckBox("OK"));
        
        TabPane tabPane = new TabPane();

        Tab tab0 = new Tab("blue");
        tab0.setContent(new Rectangle(200,200, Color.BLUE));
        
        ContextMenu contextMenu = new ContextMenu(); 
        MenuItem menuItem1 = new MenuItem("menu item 1"); 
        MenuItem menuItem2 = new MenuItem("menu item 2"); 
        MenuItem menuItem3 = new MenuItem("menu item 3"); 
        contextMenu.getItems().add(menuItem1); 
        contextMenu.getItems().add(menuItem2); 
        contextMenu.getItems().add(menuItem3); 
        tab0.setContextMenu(contextMenu);

        Tab tab1 = new Tab("green");
        tab1.setContent(new Rectangle(200,200, Color.GREEN));
        tabPane.getTabs().addAll(tab0, tab1);
        
        BorderPane masterBorderPane = new BorderPane();
        masterBorderPane.setTop(tabPane);
        masterBorderPane.setBottom(table);
        root.setCenter(masterBorderPane);
        
        Rectangle2D screenRectangle = Screen.getPrimary().getBounds();
        double screenWidth = screenRectangle.getWidth();
        double screenHeight = screenRectangle.getHeight();


		primaryStage.setScene(scene);
		primaryStage.setTitle("OneSql");
		primaryStage.setHeight(screenHeight);
		primaryStage.setWidth(screenWidth);
		primaryStage.setX(10);
		primaryStage.setY(10);
		primaryStage.show();
	}
	
	private ObservableList<Person> buildTableData() {
		ObservableList<Person> tableData = 
                FXCollections.observableArrayList(
                    new Person("Jacob", "Smith", "jacob.smith@example.com"),
                    new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                    new Person("Ethan", "Williams", "ethan.williams@example.com"),
                    new Person("Emma", "Jones", "emma.jones@example.com"),
                    new Person("Michael", "Brown", "michael.brown@example.com")
                    
                    ,new Person("Jacob", "Smith", "jacob.smith@example.com"),
                    new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                    new Person("Ethan", "Williams", "ethan.williams@example.com"),
                    new Person("Emma", "Jones", "emma.jones@example.com"),
                    new Person("Michael", "Brown", "michael.brown@example.com")
                    
                    ,new Person("Jacob", "Smith", "jacob.smith@example.com"),
                    new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                    new Person("Ethan", "Williams", "ethan.williams@example.com"),
                    new Person("Emma", "Jones", "emma.jones@example.com"),
                    new Person("Michael", "Brown", "michael.brown@example.com")
                    
                    ,new Person("Jacob", "Smith", "jacob.smith@example.com"),
                    new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                    new Person("Ethan", "Williams", "ethan.williams@example.com"),
                    new Person("Emma", "Jones", "emma.jones@example.com"),
                    new Person("Michael", "Brown", "michael.brown@example.com")
                    
                    ,new Person("Jacob", "Smith", "jacob.smith@example.com"),
                    new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                    new Person("Ethan", "Williams", "ethan.williams@example.com"),
                    new Person("Emma", "Jones", "emma.jones@example.com"),
                    new Person("Michael", "Brown", "michael.brown@example.com")
                );
		return tableData;
	}

}
